package com.sto.stoapp.activity.sales;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.adapter.SalesSignNamesAdapter;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.widget.StoHomeActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wj on 2016-12-05.
 */

public class SalesSignHelpSelectNames extends StoHomeActivity implements View.OnClickListener {

    private ListView listView;
    private EditText nameET;
    private Button addBtn,deleteBtn,returnBtn;
    private List<String> names=new ArrayList<String>();
    private SalesSignNamesAdapter adapter;

    private int deleteItem=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sales_select_names);
        this.setTitleBottom(R.string.select_title, HomeBroadAction.SALES_MAN_BROAD);

        listView= (ListView) findViewById(R.id.list_view);
        nameET= (EditText) findViewById(R.id.et_name);

        addBtn= (Button) findViewById(R.id.btn_add);
        deleteBtn= (Button) findViewById(R.id.btn_delete);
        returnBtn= (Button) findViewById(R.id.btn_return);

        addBtn.setOnClickListener(this);
        deleteBtn.setOnClickListener(this);
        returnBtn.setOnClickListener(this);

        names.addAll(StoFilePreference.getInstance(this).getSignNames());
        adapter=new SalesSignNamesAdapter(this,names);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                deleteItem=position;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_add:

                String name=nameET.getText().toString().trim();
                if("".equals(name)){
                    Toast.makeText(SalesSignHelpSelectNames.this ,getText(R.string.select_add_null)
                                    ,Toast.LENGTH_SHORT).show();
                    return;
                }
                if(name.length()>14){
                    Toast.makeText(SalesSignHelpSelectNames.this ,getText(R.string.select_add_error)
                            ,Toast.LENGTH_SHORT).show();
                    return;
                }
                names.add(name);
                StoFilePreference.getInstance(this).setSignNames(names);
                Toast.makeText(SalesSignHelpSelectNames.this ,getText(R.string.select_add_sucess)
                        ,Toast.LENGTH_SHORT).show();

                adapter.notifyDataSetChanged();
                nameET.setText("");

                break;
            case R.id.btn_delete:

//                Toast.makeText(SalesSignHelpSelectNames.this ,deleteItem+""
//                        ,Toast.LENGTH_SHORT).show();

                if(deleteItem<4){
                    Toast.makeText(SalesSignHelpSelectNames.this ,"系统配置，无法删除..."
                            ,Toast.LENGTH_SHORT).show();
                    return;
                }

                names.remove(deleteItem);
                StoFilePreference.getInstance(this).setSignNames(names);
                Toast.makeText(SalesSignHelpSelectNames.this ,getText(R.string.select_delete_sucess)
                        ,Toast.LENGTH_SHORT).show();

                adapter.notifyDataSetChanged();
                nameET.setText("");

                break;
            case R.id.btn_return:

                finish();

                break;
        }
    }
}
